﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using STF.Blog.Common;
using STF.Blog.IService;
using STF.Blog.WebApp.Areas.ForumManage.Filters;
using STF.Blog.WebApp.Areas.ForumManage.Models.Seos;
using STF.Blog.WebApp.Areas.ForumManage.Models.WebMenu;
using X.PagedList;

namespace STF.Blog.WebApp.Areas.ForumManage.Controllers
{
    [Area("ForumManage")]
    [Login]
    public class SeosController : Controller
    {
        private ISeosService _iseosSvc;
        private IWebMenuService _webMenuSvc;
        public SeosController(ISeosService iseosSvc, IWebMenuService webMenuSvc)
        {
            _iseosSvc = iseosSvc;
            _webMenuSvc = webMenuSvc;
        }

        [HttpGet]
        public async Task<IActionResult> Index(string search, int page = 1)
        {
            search = string.IsNullOrEmpty(search) ? "" : search;
            var data = await _iseosSvc.GetByNameAsync(search);
            var dataList = new List<SeosListViewModel>();

            foreach (var item in data) 
            {
                var model = new SeosListViewModel
                {
                    Id = item.Id,
                    Title = item.Title,
                    KeyWord = item.KeyWord,
                    Description = item.Description,
                    WebMenuTitle = await GetWebMenuTitle(item.WebMenuId),
                    CreateTime = item.CreateTime,
                    UpdateTime = item.UpdateTime
                };
                dataList.Add(model);
            }
            ViewBag.Search = search;
            var pages = await dataList.ToPagedListAsync(page,PageConfig.PageSize);
            return View(pages);
        }

        [HttpGet]
        public async Task<IActionResult> Add() 
        {
            await BindWebMenuList(null);
            return View(new SeosAddViewModel());
        }

        [HttpPost]
        public async Task<IActionResult> Add(SeosAddViewModel model) 
        {
            if (ModelState.IsValid) 
            {
                await _iseosSvc.AddUsersAsync(model.Title,model.KeyWord,model.Description,model.WebMenuId);
                return Redirect("~/ForumManage/Seos/Index");
            }
            return View(model);
        }

        [HttpGet]
        public async Task<IActionResult> Update(int id) 
        {
            var data = await _iseosSvc.GetByIdAsync(id);
            await BindWebMenuList(data.WebMenuId);

            return View(new SeosUpdateViewModel 
            {
                Id = data.Id,
                Title = data.Title,
                KeyWord = data.KeyWord,
                Description = data.Description,
                WebMenuId = data.WebMenuId
            });
        }

        [HttpPost]
        public async Task<IActionResult> Update(SeosUpdateViewModel model) 
        {
            if (ModelState.IsValid) 
            {
                await _iseosSvc.UpdateUsersAsync(model.Id,model.Title,model.KeyWord,model.Description,model.WebMenuId);
                return Redirect("~/ForumManage/Seos/Index");
            }
            return View(model);
        }

        [HttpPost]
        public async Task<IActionResult> Delete(int id) 
        {
            if (ModelState.IsValid) 
            {
                await _iseosSvc.PutTrashAsync(id);
                return Redirect("~/ForumManage/Seos/Index");
            }
            return View();
        }

        private async Task<string> GetWebMenuTitle(int id) 
        {
            var data = await _webMenuSvc.GetByIdAsync(id);
            return data == null ? "" : data.Title;
        }

        private async Task BindWebMenuList(int? webMenuId) 
        {
            var data = await _webMenuSvc.GetAllUsersAsync();
            var dataList = data.Select(wm => new WebMenuListViewModel
            {
                Id = wm.Id,
                Title = wm.Title
            });
            if(webMenuId == null)
                ViewBag.WebList = new SelectList(dataList,"Id","Title");
            else
                ViewBag.WebList = new SelectList(dataList, "Id", "Title",webMenuId);
        }

    }
}
